﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Perceiveit.Xml.Converters
{
    public class NullableXConverter<T> : XConverter<T?> where T : struct
    {
        private XConverter<T> InnerConverter;

        public NullableXConverter(XConverter<T> converter)
        {
            this.InnerConverter = converter;
        }

        public override string ToString(T? value)
        {
            if (value.HasValue)
                return InnerConverter.ToString(value.Value);
            else
                return string.Empty;
        }

        public override T? FromString(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            else
                return new Nullable<T>(this.InnerConverter.FromString(value));
        }

    }
}
